package com.greenlaw110.cimple.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


import act.app.AppContext;
import com.greenlaw110.cimple.model.Article;
import com.greenlaw110.cimple.model.Cate;
import com.greenlaw110.cimple.util.StringUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import org.osgl.aaa.NoAuthenticate;
import org.osgl.mvc.annotation.Before;

import static act.controller.Controller.Util.*;

@NoAuthenticate
public class IndexController {

    @Before
    public void intercept(AppContext context) {
        List<Article> tagArt = Article.getAllTag();
        String tagStr = "";
        for (Article art : tagArt) {
            System.out.println(art.get("tags"));
            tagStr += art.get("tags") + ",";
        }
        String tags = StringUtils.contactTags(tagStr);
        System.out.println(tags);
        List<Article> newArt = Article.getNewArticle();
        List<Cate> cateLists = Cate.getAllCate();
        context.renderArg("cateLists", cateLists);
        context.renderArg("newArt", newArt);
        context.renderArg("allTags", tags);
    }

    public void index(int pageNum) {
        if (0 == pageNum) pageNum = 1;
        String title = "首页";
        Page<Article> pageArt = Article.getArticleByPageIndex(pageNum);
        for (int i = 0; i < pageArt.getList().size(); i++) {
            Article art = pageArt.getList().get(i);
            art.set("content", StringUtils.cleanHtmlTags(art.getStr("content")));
        }
        render(title, pageArt);
    }

    public void tag(String tag, int pageNum) {
        if (0 == pageNum) pageNum = 1;
        try {
            tag = URLDecoder.decode(tag, "utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        tag = tag.trim();
        System.out.println(tag + "-------------------");
        Page<Article> pageArt = Article.getArticleByTag(tag, pageNum);
        for (int i = 0; i < pageArt.getList().size(); i++) {
            Article art = pageArt.getList().get(i);
            art.set("content", StringUtils.cleanHtmlTags(art.getStr("content")));
        }
        String title = tag;
        render(title, pageArt);
    }

    public void cate(String cate, int pageNum) {
        if (0 == pageNum) pageNum = 1;
        try {
            cate = URLDecoder.decode(cate, "utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        cate = cate.trim();
        System.out.println(cate + "-------------------");
        Page<Article> pageArt = Article.getArticleByCateId(Cate.getCateIdByAlias(cate), pageNum);
        for (int i = 0; i < pageArt.getList().size(); i++) {
            Article art = pageArt.getList().get(i);
            art.set("content", StringUtils.cleanHtmlTags(art.getStr("content")));
        }
        String title = Cate.getCateNameByAlias(cate);
        render(cate, pageArt, title);
    }

    public void article(String transTitle) {
        Article art = Article.getArticleByTransTitle(transTitle);
        if (art == null) {
            art = Article.getArticleById(transTitle);
        }
        String title = art.getStr("title");
        render(title, art);
    }
}